package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.constants.SystemConstant;
import cn.stylefeng.guns.modular.gridsystem.entity.WpUploadLog;
import cn.stylefeng.guns.modular.gridsystem.enums.LogExceptionEnum;
import cn.stylefeng.guns.modular.gridsystem.enums.SystemParamExceptionEnum;
import cn.stylefeng.guns.modular.gridsystem.mapper.WpUploadLogMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.WpUploadLogParam;
import cn.stylefeng.guns.modular.gridsystem.model.result.WpUploadLogResult;
import cn.stylefeng.guns.modular.gridsystem.service.WpUploadLogService;
import cn.stylefeng.guns.modular.gridsystem.utils.OptionalBean;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 倾角上传日志 服务实现类
 *
 * @author 乔李莱莱
 * @date 2023/10/16
 */
@Service
public class WpUploadLogServiceImpl extends ServiceImpl<WpUploadLogMapper, WpUploadLog> implements WpUploadLogService {

    /**
     * 分页查询倾角上传日志
     *
     * @param wpUploadLogParam gatewayId:网关id
     * @return PageResult<WpUploadLogResult> 倾角上传日志分页结果
     * @author 乔李莱莱
     * @since 2023/10/16
     */
    @Override
    public PageResult<WpUploadLogResult> page(WpUploadLogParam wpUploadLogParam) {
        //查询分页结果
        return new PageResult<>(baseMapper.pageList(PageFactory.defaultPage(), wpUploadLogParam));
    }

    /**
     * 添加倾角上传日志
     *
     * @param wpUploadLogParam dip  倾角
     *                         channelId    网关通道id
     * @author 乔李莱莱
     * @since 2023/10/16
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(WpUploadLogParam wpUploadLogParam) {
        //入参不能为空
        OptionalBean.ofNullable(wpUploadLogParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        WpUploadLog wpUploadLog = new WpUploadLog();
        BeanUtil.copyProperties(wpUploadLogParam, wpUploadLog);
        //设置用户和创建时间
        wpUploadLog.setCreateUser(LoginContextHolder.me().getSysLoginUser().getId());
        wpUploadLog.setCreateTime(new Date());
        save(wpUploadLog);
    }

    /**
     * 删除倾角上传日志
     *
     * @param wpUploadLogParam id 主键id
     * @author 乔李莱莱
     * @since 2023/10/16
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(WpUploadLogParam wpUploadLogParam) {
        //判断入参为空
        OptionalBean.ofNullable(wpUploadLogParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        OptionalBean.ofStr(wpUploadLogParam.getId());
        //删除倾角上传日志
        removeById(wpUploadLogParam.getId());
    }

    /**
     * 编辑倾角上传日志
     *
     * @param wpUploadLogParam id 主键id
     *                         dip 倾角
     *                         channelId 网关id
     * @author 乔李莱莱
     * @since 2023/10/16
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(WpUploadLogParam wpUploadLogParam) {
        //入参不能为空
        OptionalBean.ofNullable(wpUploadLogParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        WpUploadLog wpUploadLog = new WpUploadLog();
        BeanUtil.copyProperties(wpUploadLogParam, wpUploadLog);
        //设置修改人和修改时间
        wpUploadLog.setUpdateUser(LoginContextHolder.me().getSysLoginUser().getId());
        wpUploadLog.setUpdateTime(new Date());
        updateById(wpUploadLog);
    }

    /**
     * 查看详情倾角上传日志
     *
     * @param wpUploadLogParam id:网关主键id
     * @return WpUploadLogResult
     * @author 乔李莱莱
     * @since 2023/10/16
     */
    @Override
    public WpUploadLogResult detail(WpUploadLogParam wpUploadLogParam) {
        return queryWpUploadLog(wpUploadLogParam);
    }

    /**
     * 获取最新倾角
     *
     * @param wpUploadLogParam channelId 网关通道id
     *                         createTime 创建时间
     * @return WpUploadLog
     * @author 乔李莱莱
     * @since 2023/10/16
     */
    @Override
    public WpUploadLog getNewOne(WpUploadLogParam wpUploadLogParam) {
        //判断入参为空
        OptionalBean.ofNullable(wpUploadLogParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        //获取最新的倾角日志
        LambdaQueryWrapper<WpUploadLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(wpUploadLogParam.getChannelId()), WpUploadLog::getChannelId, wpUploadLogParam.getChannelId());
        queryWrapper.orderByDesc(WpUploadLog::getCreateTime);
        queryWrapper.last(SystemConstant.SQL_LIMIT);
        return getOne(queryWrapper);
    }

    /**
     * 根据id获取倾角上传日志
     *
     * @param wpUploadLogParam id 倾角id
     * @return WpUploadLogResult
     * @author 乔李莱莱
     * @since 2023/10/16
     */
    private WpUploadLogResult queryWpUploadLog(WpUploadLogParam wpUploadLogParam) {
        //判断入参为空
        OptionalBean.ofNullable(wpUploadLogParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        OptionalBean.ofStr(wpUploadLogParam.getId());
        //根据id查询倾角上传日志
        WpUploadLog wpUploadLog = getById(wpUploadLogParam.getId());
        OptionalBean.ofNullable(wpUploadLog).orElseThrow(() -> new ServiceException(LogExceptionEnum.NOT_EXISTED));
        WpUploadLogResult wpUploadLogResult = new WpUploadLogResult();
        BeanUtil.copyProperties(wpUploadLog, wpUploadLogResult);
        return wpUploadLogResult;
    }
}
